package com.kingyun.gpsinspection.purificationservice.services.iam.innerservice.qhse.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.iam.qhse.ContingencyDispositionMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.BootstrapSearchBean;
import com.kingyun.gpsinspection.purificationservice.facade.model.iam.qhse.ContingencyDisposition;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.IamFileService;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.qhse.ContingencyDispositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @version V1.0
 * @Package com.cqhilink.iot.integratedapplicationmanagement.services.innerservice.impl.qhse
 * @Description: (应急处理结果服务实现)
 * @author: lutianbo
 * @date: 2016/11/29,0029
 */
public class ContingencyDispositionServiceImpl implements ContingencyDispositionService{
    @Autowired
    private ContingencyDispositionMapper dispositionMapper;
    @Autowired
    private IamFileService iamFileService;

    /**
     * 删除
     * @param conDisIds
     * @return
     */
    @Override
    public int deleteByPrimaryKey(List<String> conDisIds) {
        if (conDisIds.size()>0){
           return dispositionMapper.deleteByPrimaryKey(conDisIds);
        }
        return 0;
    }

    /**
     * 新增
     * @param record
     * @param uploadFiles 需要上传的文件
     * @param deletedFiles hbase中需要删除的文件
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int insertSelective(ContingencyDisposition record,String uploadFiles,String deletedFiles) {
        // 插入数据成功之后，返回的条数
        int resultCount = 0;
        // 判断数据是否为空
        if(null == record){
            return  resultCount;
        }
        // 保存业务数据
        resultCount += dispositionMapper.insertSelective(record);
        // 保存附件
        resultCount += iamFileService.newInsertFile(uploadFiles,deletedFiles,record.getContingencyDispositionlId());

        return  resultCount;
    }

    /**
     * 根据id查询
     * @param conDisId
     * @return
     */
    @Override
    public ContingencyDisposition selectByPrimaryKey(String conDisId){
        if(null != conDisId){
            return  dispositionMapper.selectByPrimaryKey(conDisId);
        }
        return null;
    }

    /**
     * 更新
     * @param record
     * @param uploadFiles 需要上传的文件
     * @param deletedFiles hbase中需要删除的文件
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int updateByPrimaryKeySelective(ContingencyDisposition record,String uploadFiles,String deletedFiles){
        // 插入数据成功之后，返回的条数
        int resultCount = 0;
        // 判断数据是否为空
        if(null == record){
            return resultCount;
        }
        // 保存业务数据
        resultCount +=  dispositionMapper.updateByPrimaryKeySelective(record);
        // 保存附件
        resultCount += iamFileService.newInsertFile(uploadFiles,deletedFiles,record.getContingencyDispositionlId());

        return  resultCount;
    }

    /**
     * 列表查询
     * @param searchBean
     * @return
     */
    @Override
    public JSONObject contingencyDisList(BootstrapSearchBean searchBean){
        // 返回结果
        JSONObject jsonObject = new JSONObject();
        // 设置分页参数
        searchBean.setLimit( searchBean.getLimit() == null ? Integer.valueOf(10) : searchBean.getLimit() );
        searchBean.setOffset( searchBean.getOffset() == null ? Integer.valueOf(0) : searchBean.getOffset() );
        // 总条数
        jsonObject.put("total",dispositionMapper.queryCount(searchBean));
        // 信息
        jsonObject.put("rows", JSON.toJSON(dispositionMapper.queryList(searchBean)));
        return  jsonObject;
    }

    /**
     * 判断应急处置名是否存在
     * @param disName 应急处置名
     * @return true：存在 false:不存在
     */
    @Override
    public boolean isExitByName(String disName)
    {
        return dispositionMapper.isExitByName(disName) > 0 ? true : false;
    }
}
